$(document).ready(function(){
	$("#tab_pid_button").click(refresh_pids);
	
	$("#pid_table input").bind('change',function(){
		$(this).animate({backgroundColor: "#0000FF" }, 500);
	});
	
	err_pids();//not ready yet
	
	$(".pid_save").click(function(){
		var root = $(this).closest("tr");
		var name = root.attr("id");
		var p,i,d,max,min,maxEn,minEn,inv;
		p = root.find(".pid_p").val();
		i = root.find(".pid_i").val();
		d = root.find(".pid_d").val();
		max = root.find(".pid_max").val();
		min = root.find(".pid_min").val();
		maxEn = root.find(".pid_maxEn").attr("checked")=="checked";
		minEn = root.find(".pid_minEn").attr("checked")=="checked";
		inv = root.find(".pid_inv").attr("checked")=="checked";
		$.ajax("http://localhost:8080/at_changePID?name="+name+"&p="+p+"&i="+i+
			"&d="+d+"&max="+max+"&min="+min+"&maxEn="+maxEn+"&minEn="+minEn+"&inv="+inv)
			.success(refresh_pids);
	});
	
	function err_pids()
	{
		var list = new Array("roll","heading","pitch","vrate","altitude","throttle","slip");
		var namesNum = new Array("p","i","d","max","min");//numbers
		var namesChk = new Array("maxEn","minEn","inv");//checkboxes
		for (var x = 0; x < list.length; x++)
		{
			var name = list[x];
			//var section = $(xml).find(name);
			for (var y = 0; y < namesNum.length; y++)
			{
				var n = namesNum[y];
				var fld = $("#pid_"+name+" .pid_"+n);
				fld.attr("disabled",true);
				fld.animate({backgroundColor: "#000000" }, 500);
			}
			for (var y = 0; y < namesChk.length; y++)
			{
				var n = namesChk[y];
				var chkbx = $("#pid_"+name+" .pid_"+n);
				chkbx.attr("disabled",true);
				chkbx.button("refresh");
			}
			var save_btn = $("#pid_"+name+" .pid_save");
			save_btn.attr("disabled",true);
			save_btn.button("refresh");
		}
	}
	
	function refresh_pids()
	{
		$.ajax({
			type: "GET",
			url: "http://localhost:8080/at_getPIDs",
			dataType: "xml",
			beforeSend: err_pids(),//this tells the user the data is not ready yet (if at all)
			success: function(xml) {
				if ($(xml).find("error").text() != "False"){return;}//Oh no
				var list = new Array("roll","heading","pitch","vrate","altitude","throttle","slip");
				var namesNum = new Array("p","i","d","max","min");//numbers
				var namesChk = new Array("maxEn","minEn","inv");//checkboxes
				for (var x = 0; x < list.length; x++)
				{
					var name = list[x];
					var section = $(xml).find(name);
					for (var y = 0; y < namesNum.length; y++)
					{
						var n = namesNum[y];
						var fld = $("#pid_"+name+" .pid_"+n);
						fld.val(section.find(n).text());
						fld.removeAttr("disabled");
						fld.animate({backgroundColor: "#FFFFFF" }, 500);//good to go
					}
					for (var y = 0; y < namesChk.length; y++)
					{
						var n = namesChk[y];
						var chkbx = $("#pid_"+name+" .pid_"+n);
						//alert(chkbx);
						chkbx.attr("checked",section.find(n).text()=="True");
						chkbx.removeAttr("disabled");
						chkbx.button("refresh");
					}
					var save_btn = $("#pid_"+name+" .pid_save");
					save_btn.removeAttr("disabled");
					save_btn.button("refresh");
				}
			}
		});
	}
});
